package engine;


/*
 * So2al if we got inst. from the middle hane3mel eh sa3tha??
 * 
 * 
 * 
 * 
 */

public class InstructionBuffer {
	int size;
	public Instruction[] instructions;
	public int basePc;
	int head;
	int tail;
	boolean[] valid;
	String[] instStrings;
	public InstructionBuffer(int size, int basePc) {
		this.size=size;
		this.basePc=basePc;
		instructions=new Instruction[size];
		head=0;
		tail=0;
		valid=new boolean[size];
		instStrings=new String[size];
	}
	public boolean addInstruction(Instruction inst,String string,int fetch) {
		if(valid[tail]) {
			return false;
		}
			Instruction inst1=new Instruction();
			inst1.imm=inst.imm;
			inst1.opcode=inst.opcode;
			inst1.rs=inst.rs;
			inst1.rd=inst.rd;
			inst1.rt=inst.rt;
			inst1.type=inst.type;
			inst1.instString=string;
			inst1.fetch=fetch;
			instructions[tail]=inst1;
			instStrings[tail]=string;
			valid[tail]=true;
			tail=(tail+1)%size;
			
		return true;
	}
	public Instruction getInstruction(int pc) {
		int add=pc-basePc;
		if(add<0 || add>=size) {
			return null;
		} else {
			Instruction inst1=new Instruction();
			Instruction inst=instructions[add];
			inst1.imm=inst.imm;
			inst1.opcode=inst.opcode;
			inst1.rs=inst.rs;
			inst1.rd=inst.rd;
			inst1.rt=inst.rt;
			inst1.type=inst.type;
			inst1.instString=inst.instString;
			inst1.fetch=inst.fetch;
			return inst1;
		}
	}
}
